package com.chaoxing.controller;


import com.chaoxing.entity.CommonResult;
import com.chaoxing.entity.Course;
import com.chaoxing.entity.User;
import com.chaoxing.service.UserCourseService;
import com.chaoxing.service.UserService;
import com.chaoxing.util.PageUtil;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserCourseController {
    @Resource
    private UserCourseService userCourseService;
    @Resource
    private UserService userService;
    /**
     * 学生
     */
    //查看自己所有的课程(通过user_course表中的userid，获取courseid)
    @RequestMapping("/queryAllCoursesBySt")
    public Map<String, List<Course>> queryAllCoursesBySt(@RequestBody User user) {
        Map<String, List<Course>> map = new HashMap<>();
        List<Course> courses = userCourseService.queryAllCoursesBySt(user.getId());
        if (courses.size() != 0) {
            map.put("success", courses);
            return map;
        } else
            map.put("null", courses);
        return map;
    }

    //查看某一门课的详细信息
    //查看课程信息(通过user_course表中的courseid，获取courseid对应的course)
    @RequestMapping("/queryCourseBySt")
    public Map<String, Course> queryCourseBySt(Integer courseid) {
        Map<String, Course> map = new HashMap<>();
        Course course = userCourseService.queryCourseBySt(courseid);
//        System.out.println(course);
        if (course != null) {
            map.put("msg", course);
        }
        map.put("msg", course);
        return map;
    }

    //学生选课
    /*
    首先获取自己所有的课程列表，将即将选取的课成与自己现在的课程列表对比，如果该课程已经存在列表中，将不能选课，
        否则将这门课程加入到自己的课程列表中。
     */
    @RequestMapping("/insertCourseBySt")
    @Transactional
    public Map<String, String> insertCourseBySt(HttpServletRequest request,Integer courseid) {
        HttpSession session = request.getSession(false);
        Map<String, String> map = new HashMap<>();
        if(session == null){
            map.put("errMsg","尚未登录！");
            return map;
        }else {
            User user = (User) session.getAttribute("User");
            //得到该user所有的课程id
            List<Integer> courseList = userCourseService.queryCourseIdBySt(user.getId());
            if (courseList.size() == 0) {
                //如果courseList 课程集合 为空
                int i = userCourseService.insertCourseBySt(user.getId(), courseid);
                map.put("msg", i + "");
            }
            //否则不为空
            else {
                if (!courseList.contains(courseid)) {
                    //如果courseList 课程集合里面没有所选课程id
                    int i = userCourseService.insertCourseBySt(user.getId(), courseid);
                    map.put("msg", "选课成功!");
                    return map;

                } else {
                    //如果courseList 课程集合里面有所选课程id
                    map.put("msg", "你已选取该课程，选课失败!");
                }
            }
            return map;
        }

    }

    /**
     * 老师
     */
    //查看自己所有的课程(通过user_course表中的userid，获取courseid)
    @RequestMapping("/queryAllCoursesByTe")
    public Map<String, List<Course>> queryAllCoursesByTe(User user) {
        Map<String, List<Course>> map = new HashMap<>();
        List<Course> courses = userCourseService.queryAllCoursesByTe(user.getId());
        if (courses.size() != 0) {
            map.put("success", courses);
            return map;
        } else
            map.put("null", courses);
        return map;
    }

    //老师查看课程的详细信息
    @RequestMapping("/queryCourseByTe")
    public Map<String, Course> queryCourseByTe(Integer courseid) {
        Map<String, Course> map = new HashMap<>();
        String msg = "查询失败!";
        Course course = userCourseService.queryCourseByTe(courseid);
        if (course != null) {
            msg = "查询成功!";
            map.put(msg, course);
        }
        map.put(msg, course);
        return map;
    }

    //老师可以查看自己教授的课程的学生(做分页)
    @RequestMapping("/queryAllStudentByTe")
    public Map<String, List<User>> queryAllStudentByTe(Integer courseid, int pageNo) {
        Map<String, List<User>> map = new HashMap<>();
        String msg = "查看成功";
        int index = PageUtil.pageNoToIndex(pageNo);
        List<User> userList = userCourseService.queryAllStudentByTe(courseid, index, PageUtil.PAGE_SIZE);
        if (userList != null) {
            map.put("msg", userList);
        } else {
            map.put("查询失败", userList);
            return map;
        }
        return map;
    }


    /**
     * 管理员
     */
    //给老师发布要教授的课程
    @RequestMapping("/insertCourseByTe")
    @Transactional
    public Map<String, Integer> insertCourseByTe(Integer userid, Integer courseid) {
        Map<String, Integer> map = new HashMap<>();
        String msg = "";
        //得到该user所有的课程id
        List<Integer> courseList = userCourseService.queryCourseIdByTe(userid);
        if (courseList.size() == 0) {
            //如果courseList 课程集合 为空 ,可以发布该课程
            int i = userCourseService.insertCourseByTe(userid, courseid);
            msg = "老师发布教授的课程成功!";
            map.put(msg, i);
        } else {//否则不为空
            if (!courseList.contains(courseid)) {
                //如果courseList 课程集合里面没有所选课程id
                int i = userCourseService.insertCourseByTe(userid, courseid);
                msg = "老师发布教授的课程成功!";
                map.put(msg, i);
                return map;

            } else {
                //如果courseList 课程集合里面有所选课程id
                msg = "您已发布该课程!";
                map.put(msg, 0);
            }
        }
        return map;
    }

    //管理员给老师或者学生删除一门课程
    @RequestMapping("/deleteStudentByAd")
    public Map<String, Integer> deleteStudentByAd(Integer courseid, Integer userid) {
        Map<String, Integer> map = new HashMap<>();
        String msg = "删除失败,请检查该学生是否在列表中";
        int i = userCourseService.deleteStudentByAd(courseid, userid);
        System.out.println(i);
        if (i == 1) {
            msg = "删除成功!";
            map.put(msg, i);
            return map;
        }
        map.put(msg, i);
        return map;
    }

    //管理员删除一门课程
    @Transactional
    @RequestMapping("/deleteCourseByAd")
    public Map<String, String> deleteCourseByAd(Integer id, HttpServletRequest request) {
        Map<String, String> map = new HashMap<>();
        User user = null;
        HttpSession session = request.getSession(false);
        if (session == null) {
            map.put("msg", "您尚未登录");
            return map;
        } else {
            user = (User) session.getAttribute("User");
            String region = user.getRegion();
            if ("admin".equals(region)) {
                int i = userCourseService.selectAll(id);
                if (i == 0) {
                    //直接删除课程
                    userCourseService.deleteCourseByAd(id);
                    map.put("msg", "删除成功");
                    return map;
                } else {
                    userCourseService.deleteAll(id);
                    userCourseService.deleteCourseByAd(id);
                    map.put("msg", "删除成功");
                    return map;
                }
            } else {
                map.put("msg", "删除失败,您没有操作权限！");
            }
            return map;
        }
    }

    //管理员批量删除
    @RequestMapping("/deleteAllCoursesByAd")
    public Map<String, String> deleteAllCoursesByAd(HttpServletRequest request) {
        String[] ids = request.getParameterValues("id");
        List<Integer> list = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        User user = null;
        HttpSession session = request.getSession(false);
        if (session == null) {
            map.put("msg", "您尚未登录");
            return map;
        } else {
            user = (User) session.getAttribute("User");
            String region = user.getRegion();
            if ("admin".equals(region)) {
                //可以删除课程
                for (int i = 0; i < ids.length; i++) {
                    list.add(Integer.valueOf(ids[i]));
                }
                userCourseService.deleteAllUsersByAd(list);
                userCourseService.deleteAllCoursesByAd(list);
                map.put("msg", "删除成功");
                return map;
            } else {
                map.put("msg", "删除失败,您没有操作权限！");
            }
            return map;
        }
    }

    /*查询所有老师课程*/
    @RequestMapping("/selectTC.do")
    public Map<String,List<CommonResult>> selectAllCourseAndTeacher(){
        Map<String,List<CommonResult>> map = new HashMap<>();
        List<CommonResult> courses = userCourseService.queryCoursesAndTeachByTe();
        for (CommonResult c : courses){
            User user = userService.queryUser(c.getUserid());
            c.setName(user.getName());
            c.setPhone(user.getPhone());
        }
        if(courses.size() > 0 ){
            map.put("resp",courses);
            return map;
        }
        map.put("errMsg",null);
        return map;
    }
}
